package com.compress4j.arithmetic.ppm;

import java.util.AbstractSet;
import java.util.Arrays;
import java.util.Iterator;

import sun.reflect.generics.reflectiveObjects.NotImplementedException;

public class CompressionItem {

	private int theByte = -1;
	
	private CompressionItem(int byteVal) {
		theByte = byteVal;
	}	
	/**
	 * @return the contained byte as an int
	 */
	public int getByte(){
		return theByte;
	}
	/**
	 * for debug purpose
	 */
	public String toString() {
		return new String(new byte[]{(byte)theByte});
	}

	/**
	 * please don't modify the content of this array
	 */
	public final static CompressionItem[] ALL_BYTE;
	
	static {
		ALL_BYTE = new CompressionItem[256]; 
		for(int i=0;i<256;i++){
			ALL_BYTE[i] = new CompressionItem(i);
		}
	}
	public static CompressionItem get(int i){
		return ALL_BYTE[i];
	}
	/**
	 * Optimized Set for CompressionItem : minimal implementation
	 * @author b.pierre
	 */
	public static class Set extends AbstractSet<CompressionItem>{

		boolean[] datas = new boolean[256];
		
		@Override
		public boolean add(CompressionItem e) {
			int idx = e.getByte();
			if(datas[idx]) return false;
			datas[idx] = true;
			return true;
		}
		@Override
		public void clear() {
			Arrays.fill(datas, false);
		}

		@Override
		public boolean contains(Object o) {
			int idx = ((CompressionItem)o).getByte();
			if(idx<0) return false;
			return datas[idx];
		}
		@Override
		public Iterator<CompressionItem> iterator() {
			throw new NotImplementedException();
		}
		@Override
		public int size() {
			throw new NotImplementedException();
		}
		public String toString(){
			StringBuilder b = new StringBuilder();
			for(int i=0;i<datas.length;i++){
				if(datas[i]){
					b.append(",");
					b.append(ALL_BYTE[i]);
				}
			}
			return b.toString();
		}
	};
}
